var app = getApp();
var self = "";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSearchAnim: "", //使用说明动画参数
    actImg: "../../images/index/actImg.png",
    itemImg1: "https://zhuanyoyo.oss-us-west-1.aliyuncs.com/customerApplet/coupons/noUsedCoupon.png", //优惠背景图片
    noActImg1: "https://zhuanyoyo.oss-us-west-1.aliyuncs.com/customerApplet/coupons/usedConpon.png", //已使用优惠券
    navAct:true,
    storeDisCountList:[],
    navBarIndex: 2, //选中的导航栏
    redAnim:0, //红色按钮动画0关闭1右滑2左滑动
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self = this;
    this.changeNav({currentTarget:{
      dataset:{
        params: "act"
      }
    }});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },	

  //自定义事件
  //初始化页面
  initData(cpStatus){
    wx.showLoading({
      title: 'loading...',
      mask:true
    })
    if (cpStatus == 1){
      wx.request({
        url: app.globalData.officialDomain + 'coupon/selectCouponListByCust',
        data: { couponStatus: cpStatus, custId: wx.getStorageSync("userId") },
        success: function (res) {
          console.log(res)
          var resData = res.data;
          if(res.statusCode == "200"){
            if (resData.errorCode = "0000") {
              resData.data.map((item, index) => {
                try {
                  item.shopImage = JSON.parse(item.shopImage);
                } catch (error) {
                  item.shopImage = [];
                }
              })
              wx.hideLoading();
              self.setData({
                storeDisCountList: resData.data
              })
            }
          }else{
            wx.showToast({
              title: 'Server Error！',
              icon:'none'
            })
            wx.hideLoading();
          }
        },
        complete:function(){
            wx.hideLoading();
        }
      })
    }else{
      wx.request({
        url: app.globalData.officialDomain + 'custShare/getCustCanExchangeCouponList',
        data: {custId: wx.getStorageSync("userId") },
        success: function (res) {
          console.log('custShare/getCustCanExchangeCouponList')
          console.log(res)
          var resData = res.data;
          if (res.statusCode == "200") {
            if (resData.errorCode = "0000") {
              resData.data.map((item,index)=>{
                try{
                  item.shopImage = JSON.parse(item.shopImage);
                }catch(error){
                  item.shopImage = [];
                }
              })
              wx.hideLoading();
              self.setData({
                storeDisCountList: resData.data
              })
            }
          } else {
            wx.showToast({
              title: 'Server Error！',
              icon: 'none'
            })
            wx.hideLoading();
          }
        },
        complete: function () {
          wx.hideLoading();
        }
      })
    }   
  },
  //切换导航
  changeNav(e) {
    // console.log(e.currentTarget.dataset.params)
    this.navAct = e.currentTarget.dataset.params == "act" ? true : false;
    this.redAnim = e.currentTarget.dataset.params == "act" ? 2 : 1;
    this.setData({
      navAct:this.navAct,
      storeDisCountList: [],
      redAnim: this.redAnim
    })
    this.initData(this.navAct ? 0 : 1);
  },

  // v.1.7需求
  // 显示搜索框
  showSeaBtn() {
    if (this.data.showSearchAnim == "slideBot") {
      this.setData({
        showSearchAnim: "slideTop",
      })
      setTimeout(() => {
        this.setData({
          isShowSearch: true
        })
      }, 1000)
    } else {
      this.setData({
        showSearchAnim: "slideBot",
        isShowSearch: false
      })
    }
  },
  
  //跳转页面
  // 通过data-params带上路径
  toOpen2(e) {
    console.log(e.currentTarget.dataset.urls)
    var itemUrls = e.currentTarget.dataset.urls;
    var itemParams = e.currentTarget.dataset.params;
    wx.navigateTo({
      url: '../' + itemUrls + '/' + itemUrls + '?params=' + itemParams + '&isForward=2' 
    })
  },

  toOpen3(e) {
    var shopStatus = e.currentTarget.dataset.shopstatus;
    if (shopStatus == "1"){
      this.toOpen2(e);
    }else{
      wx.showToast({
        icon:'none',
        title: '此店铺已下架！',
      })
    }
  },

  //跳转到快来兑换页
  toOpenChangePage(e){
    var couponId = e.currentTarget.dataset.couponid;
    var shopId = e.currentTarget.dataset.params;    
    wx.navigateTo({
      url: '../storeDetail/storeDetail?couponId=' + couponId + '&params=' + shopId + '&isForward=2&showActionSheet4=show'
    })
  }

})